<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="笔记标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入笔记标题"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['blog:blog:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['blog:blog:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="blogList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="笔记id" align="center" prop="id" />
      <el-table-column label="笔记标题" align="center" prop="title" />
      <el-table-column label="笔记内容" align="center" prop="content" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="点赞数" align="center" prop="liked" />
      <el-table-column label="收藏数" align="center" prop="mark" />

      <el-table-column label="发布地址" align="center" prop="address" />

      <el-table-column
        label="是否不公开(否/是)"
        align="center"
        prop="isHide"
        width="250px"
      >
        <template #default="scope">
          <el-switch :disabled="true" v-model="scope.row.isHide" />
        </template>
      </el-table-column>
      <el-table-column label="查看图片" align="center" prop="mark">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['blog:blog:remove']"
            >查看</el-button
          >
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd(scope.row)"
            v-hasPermi="['blog:blog:add']"
            >审核</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['blog:blog:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改笔记管理对话框 -->
    <el-dialog
      :title="title"
      v-model="open"
      width="800px"
      append-to-body
      :close-on-click-modal="false"
      :show-close="!showBtn"
    >
      <el-form ref="blogRef" :model="form" :rules="rules" label-width="80px">
        <el-table
          :data="blogImgsList"
          :row-class-name="rowBlogImgsIndex"
          ref="blogImgs"
        >
          <el-table-column width="50" align="center" />
          <el-table-column
            label="序号"
            align="center"
            prop="index"
            width="50"
            v-if="!showBtn"
          />
            
          <el-table-column label="图片路径" prop="url" width="350" v-if="!showBtn" >
            <template #default="scope">
              <a
                :href="scope.row.url"
                style="cursor: pointer"
                @click.prevent="openPopup(scope.row.url)"
                >{{ scope.row.url }}</a
              >
            </template>
          </el-table-column>
        </el-table>

         <el-form-item
            label="是否通过(否/是)"
            align="center"
            prop="isOk"
            width="350px"
               v-if="showBtn"
          >
              <el-switch   v-model="isOk" />
          </el-form-item>
          <br>
          <br>
          
          <el-form-item
            label="审核未通过原因"
            align="center"
            prop="isOk"
            width="350px"
            v-if="!isOk&&showBtn"
          >
              <el-input  v-model="inputCause" width="150">
              </el-input>
          </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer" v-if="showBtn">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button> 
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Blog">









const showBtn = ref(false);
import $ from "jquery";
import "magnific-popup/dist/magnific-popup.css";
import "magnific-popup";
function openPopup(url) {
  $(document).ready(function () {
    $.magnificPopup.open({
      items: {
        src: url,
        type: "image",
      },
      gallery: {
        enabled: true,
      },
    });
  });
}
import {
  listBlog,
  getBlog,
  delBlog,
  addBlog,
  updateBlog,
} from "@/api/blog/blog";

const { proxy } = getCurrentInstance();

const blogList = ref([]);
const blogImgsList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const checkedBlogImgs = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    see:0,
    title: null,
    status: null,
  },
  rules: {},
});

const { queryParams, form, rules } = toRefs(data);

/** 查询笔记管理列表 */
function getList() {
  loading.value = true;
  listBlog(queryParams.value).then((response) => {
    blogList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  showBtn.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    title: null,
    content: null,
    createTime: null,
    liked: null,
    address: null,
    x: null,
    y: null,
    isHide: null,
    status: null,
    mark: null,
  };
  blogImgsList.value = [];
  proxy.resetForm("blogRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd(row) {
  const _id = row.id || ids.value;
  getBlog(_id).then((response) => {
    form.value = response.data;
    blogImgsList.value = response.data.blogImgsList;
    open.value = true;
    showBtn.value = true;
    title.value = "审核笔记";
  });
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value;
  getBlog(_id).then((response) => {
    form.value = response.data;
    blogImgsList.value = response.data.blogImgsList;
    open.value = true;
    title.value = "查看图片";
  });
}
const isOk = ref(0);
const inputCause=ref("");


import { useStore } from "vuex";
const store = useStore();
const sendMsg = () => {
  let msgObj = {
    to: form.value.username,
    content: isOk.value
      ? "您的笔记标题为："+form.value.title+"，审核通过"
      : "您的笔记标题为："+form.value.title +"，审核未通过，原因为：" + inputCause.value,
    notSelf: false,
  };
  store.state.stomp.send("/ws/success", {}, JSON.stringify(msgObj));
};

/** 提交按钮 */
function submitForm() {
  form.value.status = isOk.value?1:0;
  form.value.cause=inputCause.value;
  form.value.blogImgsList = blogImgsList.value;
  form.value.see=1;
  if (form.value.id != null) {
    updateBlog(form.value).then((response) => {
      if(isOk.value){
        proxy.$modal.msgSuccess("审核成功");
      }else{
        proxy.$modal.msgError("审核失败,原因为"+inputCause.value);
      }
      sendMsg();
      open.value = false;
      getList();
    });
  } else {
    addBlog(form.value).then((response) => {
      proxy.$modal.msgSuccess("新增成功");
      open.value = false;
      getList();
    });
  }
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除笔记管理编号为"' + _ids + '"的数据项？')
    .then(function () {
      return delBlog(_ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 笔记图片序号 */
function rowBlogImgsIndex({ row, rowIndex }) {
  row.index = rowIndex + 1;
}

/** 笔记图片添加按钮操作 */
function handleAddBlogImgs() {
  let obj = {};
  obj.url = "";
  blogImgsList.value.push(obj);
}

/** 笔记图片删除按钮操作 */
function handleDeleteBlogImgs() {
  if (checkedBlogImgs.value.length == 0) {
    proxy.$modal.msgError("请先选择要删除的笔记图片数据");
  } else {
    const blogImgss = blogImgsList.value;
    const checkedBlogImgss = checkedBlogImgs.value;
    blogImgsList.value = blogImgss.filter(function (item) {
      return checkedBlogImgss.indexOf(item.index) == -1;
    });
  }
}

/** 复选框选中数据 */
function handleBlogImgsSelectionChange(selection) {
  checkedBlogImgs.value = selection.map((item) => item.index);
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "blog/blog/export",
    {
      ...queryParams.value,
    },
    `blog_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>
